package com.qf.hospital.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.hospital.entity.SysMenu;
import com.qf.hospital.entity.SysRole;
import com.qf.hospital.entity.SysRoleExample;
import com.qf.hospital.enums.HospitalErrorEnums;
import com.qf.hospital.exception.HospitalException;
import com.qf.hospital.mapper.SysRoleMapper;
import com.qf.hospital.service.SysMenuService;
import com.qf.hospital.service.SysRoleService;
import com.qf.hospital.util.ExampleUtil;
import com.qf.hospital.vo.LayUIDTreeVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author Administrator
 * @version V1.0
 * @Project hospital
 * @Package com.qf.hospital.service.impl
 * @Description: ${TODO}
 * @Date 2021/10/18 16:24
 */


@Service
public class SysRoleServiceImpl implements SysRoleService {

    private Logger log = LoggerFactory.getLogger(SysRoleServiceImpl.class);

    @Autowired
    private SysRoleMapper roleMapper;

    @Autowired
    private SysMenuService menuService;

    @Override
    public List<SysRole> findAll() {
        return roleMapper.selectByExample(null);
    }

    @Override
    public PageInfo<SysRole> loadAllRole(String rolename, String roledesc, Integer available, Integer page, Integer limit) {
        //1.分页
        PageHelper.startPage(page, limit);
        //2.条件
        SysRoleExample example = new SysRoleExample();
        SysRoleExample.Criteria criteria = example.createCriteria();
        if (!StringUtils.isEmpty(rolename)){
            criteria.andRoleNameLike(ExampleUtil.any(rolename));
        }
        if (!StringUtils.isEmpty(roledesc)){
            criteria.andRoleDescLike(roledesc);
        }
        if (available != null){
            criteria.andAvailableEqualTo(available);
        }
        //3.查询
        List<SysRole> roleList = roleMapper.selectByExample(example);
        //4.封装
        PageInfo<SysRole> pageInfo = new PageInfo<>(roleList);
        //5.返回
        return pageInfo;
    }

    @Override
    public List<LayUIDTreeVO> initRoleMenuTreeJson(Integer roleid) {
        //1.查询全部角色信息
        List<SysMenu> menuList = menuService.findAll();
        //2.查询当前角色拥有的菜单
        List<Integer> roleMenuIds = roleMapper.findMenuByRoleId(roleid);
        //3.封装数据
        List<LayUIDTreeVO> data = new ArrayList<>();
        for (SysMenu sysMenu : menuList) {
            LayUIDTreeVO vo = new LayUIDTreeVO();
            BeanUtils.copyProperties(sysMenu, vo);
            vo.setCheckArr(roleMenuIds.contains(vo.getId())? "1":"0");
            data.add(vo);
        }
        //4.返回
        return data;
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public void saveRoleMenu(Integer roleid, Integer[] ids) {

        roleMapper.deleteByRoleId(roleid);

        if(ids != null && ids.length > 0){
            int count = roleMapper.saveRoleMenu(roleid,ids);
            if(count != ids.length){
                log.error("【分配角色菜单】 分配角色菜单异常！ roleid = {}，ids = {}",roleid, Arrays.toString(ids));
                throw new HospitalException(HospitalErrorEnums.ROLE_SAVE_ROLE_MENU_ERROR);
            }
        }

    }

    @Override
    @Transactional
    public void deleteByRoleId(Integer roleid) {
        roleMapper.deleteById(roleid);
    }

    @Override
    @Transactional
    public void updateByRoleId(SysRole role) {
        roleMapper.updateByPrimaryKeySelective(role);
    }

    @Override
    @Transactional
    public void addRole(SysRole role) {

        int count = roleMapper.insertSelective(role);
        if (count != 1){
            log.error("【添加角色信息】 添加失败 role = {} ",role);
            throw new HospitalException(HospitalErrorEnums.ROLE_ADD_ERROR);
        }
    }
}
